********************************************************************************
/**THIS FILE CREATES MAIN TABLES AND FIGURES FOR 
“The Effects of Education on Financial Outcomes: Evidence from Kenya”**
 
 Additional packages needed: cgmreg cgmwildboot2 ivreg2 xtivreg2 ranktest unique boottest
 */
 
clear all

// Set your local directory here
cd ""

global dr = "input_data/"
global d0 = "output_data/"
global dtab = "tables_figures/"


** Load in basefile with all variables created **
use "$d0/fpe_FinAccess_basefile.dta", clear
* Merge on 1997 poverty rates *
merge m:1 sub_region using "$d0/lsms_97_poverty_rates.dta", assert(3) nogen
lab var poverty_rate "1997 Poverty rate"

* Merge on FSE intensity *
merge m:1 sub_region using "$d0/fse_intensity.dta", assert(3) nogen
lab var no_sec_trans "Free secondary education intensity"
rename no_sec_trans fse

lab var dhs_2014_attend "Mean attendance of age cohort in 2014 DHS"
replace dhs_2014_attend = . if age>24

********************************************************************************
****				Table 1: Demographic Summary Statistics 				****
********************************************************************************
lab var a_14 "Education level"
lab var FPE "FPE cohort"
lab var never_attend_female "Intensity (female)"
lab var never_attend_male "Intensity (male)"
lab var poverty_rate "Poverty rate (1997)

cap file close output
file open output using "$dtab/table1.tex", write replace

foreach var of varlist a_14 some_primary completed_primary some_secondary ///
completed_secondary educ_yrs_censored age female married christian muslim  ///
mobile_phone internet_usage 											  ///
FPE never_attend never_attend_female never_attend_male potential_yrs_prim fse ///
unemployment_rate poverty_rate dhs_2014_attend {

preserve
if "`var'" == "never_attend_female" {
keep if female == 1
}
if "`var'" == "never_attend_male" {
keep if female == 0
}

local lbl: variable label `var'

su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 1
local fpe_mean = `r(mean)'
local fpe_sd = `r(sd)'
su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 0
if "`var'" != "dhs_2014_attend" {
local nofpe_mean = `r(mean)'
local nofpe_sd = `r(sd)'
}
if "`var'" != "dhs_2014_attend" {
	foreach l in fpe_mean nofpe_mean fpe_sd nofpe_sd  {
		local `l' = string(round(``l'',.001), "%9.3f")
	}
}

else if "`var'" == "dhs_2014_attend" {
	foreach l in fpe_mean  fpe_sd   {
		local `l' = string(round(``l'',.001), "%9.3f")
		local nofpe_mean = "N/A"
		local nofpe_sd = "N/A"
	}
}


file write output "`lbl' & " _tab "`fpe_mean' & " _tab "`fpe_sd' & " _tab "`nofpe_mean' & " _tab "`nofpe_sd' \\ " _n
restore
}
count if FPE == 1 &sample2 == 1
local N_FPE = `r(N)'
count if FPE == 0 &sample2 == 1 
local N_NOFPE = `r(N)'

file write output "\midrule" _n
file write output "Observations & " _tab "\multicolumn{2}{c}{`N_FPE'} & " _tab ///
	"\multicolumn{2}{c}{`N_NOFPE'} \\"  _n


file close output

********************************************************************************
****			Table 2: Financial and Economic Summary Statistics			****
********************************************************************************
lab var b2 "Financial literacy"
lab var has_formal_savings_product "Currently formal savings (incl. Mpesa)"
lab var ever_formal_savings_product "Ever formal savings (incl. Mpesa)"
lab var has_formal_loan_credit_product "Currently formal loan/credit (incl. Mpesa)"
lab var ever_formal_loan_credit_product "Ever formal loan/credit (incl. Mpesa)"
lab var improved_financial_life "Improved financially over year"

lab var time_to_bank_branch "Distance to nearest bank branch"
lab var time_to_mpesa "Distance to nearest mobile money agent"
lab var time_to_bank_agent "Distance to nearest bank agent"
lab var time_to_fin_serv_provid "Distance to nearest financial service provider"

lab var dk_time_to_bank_branch "Don't know distance to bank branch"
lab var dk_time_to_mpesa "Don't know distance to mobile money agent"
lab var dk_time_to_bank_agent "Don't know distance to bank agent"
lab var dk_time_to_fin_serv_provid "Financial service provider"

** Main outcomes **
cap file close output
file open output using "$dtab/table2A.tex", write replace

foreach var of varlist ever_banked2 banked_only_mobile2 ever_banked_mpesa_only ///
ever_banked has_bank_product2 ever_formal_savings_product has_formal_savings_product ///
ever_formal_loan_credit_product has_formal_loan_credit_product          /// 
insurance_product b2 numeracy2 any_savings informal_savings_group       ///
emergency_funds safe_place_money                        ///
retire_own_plans retire_no_plans retire_safety_net earned_any_income earned_income {

local lbl: variable label `var'

su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 1
local fpe_mean = `r(mean)'
local fpe_sd = `r(sd)'
su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 0
local nofpe_mean = `r(mean)'
local nofpe_sd = `r(sd)'

if "`var'" != "earned_income" {
	foreach l in fpe_mean nofpe_mean fpe_sd nofpe_sd  {
		local `l' = string(round(``l'',.001), "%9.3f")
	}
}

else if "`var'" == "earned_income" {
	foreach l in fpe_mean nofpe_mean fpe_sd nofpe_sd  {
		local `l' = string(round(``l'',.001), "%9.0f")
	}
}

file write output "`lbl' & " _tab "`fpe_mean' & " _tab "`fpe_sd' & " _tab "`nofpe_mean' & " _tab "`nofpe_sd' \\ " _n
}

file close output

** Primary money sources **
cap file close output
file open output using "$dtab/table2B.tex", write replace

foreach var of varlist farming_most_money employed_most_money casual_most_money self_employ_most_money ///
family_friends_most_money other_most_money {

local lbl: variable label `var'


su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 1
local fpe_mean = `r(mean)'
local fpe_sd = `r(sd)'
su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 0
local nofpe_mean = `r(mean)'
local nofpe_sd = `r(sd)'

foreach l in fpe_mean nofpe_mean fpe_sd nofpe_sd {
	local `l' = string(round(``l'',.001), "%9.3f")
}

	

file write output "`lbl' & " _tab "`fpe_mean' & " _tab "`fpe_sd' & " _tab "`nofpe_mean' & " _tab "`nofpe_sd' \\ " _n
}
file close output

** Financial Access **
cap file close output
file open output using "$dtab/table2C.tex", write replace

foreach var of varlist time_to_bank_branch time_to_mpesa time_to_bank_agent  ///
	dk_time_to_bank_branch dk_time_to_mpesa dk_time_to_bank_agent  {

local lbl: variable label `var'

su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 1
local fpe_mean = `r(mean)'
local fpe_sd = `r(sd)'
su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 0
local nofpe_mean = `r(mean)'
local nofpe_sd = `r(sd)'

foreach l in fpe_mean nofpe_mean fpe_sd nofpe_sd {
	local `l' = string(round(``l'',.001), "%9.3f")
}

file write output "`lbl' & " _tab "`fpe_mean' & " _tab "`fpe_sd' & " _tab "`nofpe_mean' & " _tab "`nofpe_sd' \\ " _n
}
file close output

********************************************************************************
****				Table 3: Years of Education (Censored at 12)			****
********************************************************************************

preserve
keep if sample2 == 1
compress
eststo clear

cgmwildboot2 educ_yrs_censored FPExintensity FPE Intensity ///
	if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(1211) ///
	null(0 0 0)

matrix A = e(p)	

eststo: reg educ_yrs_censored FPExintensity FPE Intensity if sample2 == 1 [pw = popwgt_raw], cluster(sub_region)
qui sum educ_yrs_censored if e(sample) [aw=popwgt_raw]
local lhsmean1 = string(round(`r(mean)',.001),"%9.3f")
local lhssd1 = string(round(`r(sd)',.001),"%9.3f")	
estadd matrix cgm = A
test FPExintensity
estadd local fstat = string(round(`r(F)',.01),"%9.2f")

cgmwildboot2 educ_yrs_censored FPExintensity Intensity female age_16 age_17 age_18 age_28 age_29 ///
	if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(1211) ///
	null(0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo: reg educ_yrs_censored FPExintensity Intensity female age_16 age_17 age_18 age_28 age_29 ///
	if sample2 == 1 [pw = popwgt_raw], cluster(sub_region)
estadd local cohortfe = "\checkmark"
estadd matrix cgm = A
test FPExintensity
estadd local fstat = string(round(`r(F)',.01),"%9.2f")

xi: cgmwildboot2 educ_yrs_censored FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
	if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(1211) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo: areg educ_yrs_censored FPExintensity female age_16 age_17 age_18 age_28 age_29 ///
	if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) absorb(sub_region)
estadd local srfe = "\checkmark"
estadd local cohortfe = "\checkmark"
estadd matrix cgm = A
test FPExintensity
estadd local fstat = string(round(`r(F)',.01),"%9.2f")

estout using "$dtab/table3.tex", keep(FPExintensity FPE Intensity)  ///
replace starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) ///
s(N r2_a fstat cohortfe srfe, fmt("0 3") ///
	labels("Observations" "Adjusted \(R^{2}\)" "FPEXIntensity F-stat" "Cohort FE" "Subregion FE")) ///
	prefoot("\hline") mlabels(, none) collabels(, none) style(tex) label substitute(X "$\times$") ///
	posthead("Mean Dep. Var. [SD] & \multicolumn{3}{c}{`lhsmean1' [`lhssd1']} \\" "\midrule")   
 
eststo clear

restore

********************************************************************************
****			Table 4: Reduced-Form Results for Summary Indices			****
********************************************************************************

preserve
keep if sample2 == 1
compress
eststo clear
local i = 0
foreach var of varlist fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {

local i = `i' + 1
********* Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

cgmwildboot2 `var' FPExintensity FPE Intensity ///
	[pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0)

matrix A = e(p)	

eststo: reg `var' FPExintensity FPE Intensity [pw = popwgt_raw], cluster(sub_region)
estadd matrix cgm = A

cgmwildboot2 `var' FPExintensity Intensity female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo: reg `var' FPExintensity Intensity female  age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw], cluster(sub_region)
estadd local cohortfe = "\checkmark"
estadd matrix cgm = A

xi: cgmwildboot2 `var' FPExintensity  female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
	[pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo: areg `var' FPExintensity female  age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw], ///
	cluster(sub_region) absorb(sub_region)
estadd local srfe = "\checkmark"
estadd local cohortfe = "\checkmark"
estadd matrix cgm = A

}

estout using "$dtab/table4.tex", keep(FPExintensity FPE Intensity)  ///
replace   starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) ///
s(N r2_a cohortfe srfe, fmt("0 3") ///
	labels("Observations" "Adjusted \(R^{2}\)" "Cohort FE" "Subregion FE")) ///
	prefoot("\hline") mlabels(, none) collabels(, none) style(tex) label substitute(X "$\times$")
eststo clear

restore

********************************************************************************
****			Table 5: Reduced-Form  Results for Specific Outcomes		****
********************************************************************************

preserve
keep if sample2 == 1
compress

local fin_incl = "ever_banked2 has_bank_product2 ever_formal_savings_product"
local fin_capib = "b2 numeracy2 any_savings"
local econ_self = "earned_any_income IHST_earned_income income_independence"

eststo clear
local j = 0
foreach list in "`fin_incl'" "`fin_capib'" "`econ_self'" {
local j = `j' + 1
local i = 0
foreach var of varlist `list' {
local i = `i' + 1
********* Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`j'`i': areg `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] ///
if sample2 == 1, cluster(sub_region) absorb(sub_region)

estadd matrix cgm = A
su `var' if e(sample) & ctrl == 1 [aw=popwgt_raw]
estadd scalar ctrl_mean = r(mean)
}
	
}


estout a11 a12 a13 using "$dtab/table5A_fin_incl.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	s(N r2_a ctrl_mean, fmt("0 3") labels("Observations" "Adjusted \(R^{2}\)" "Control Group Mean"))
	
estout a21 a22 a23 using "$dtab/table5B_fin_capib.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	s(N r2_a ctrl_mean, fmt("0 3") labels("Observations" "Adjusted \(R^{2}\)" "Control Group Mean"))

estout a31 a32 a33 using "$dtab/table5C_econ_self.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	s(N r2_a ctrl_mean, fmt("0 3") labels("Observations" "Adjusted \(R^{2}\)" "Control Group Mean"))	

eststo clear


restore

********************************************************************************
****	Table 6: Including Demographic, Income, and Supply-Side Controls	****
********************************************************************************

lab var b2 "Financial literacy"
lab var ever_banked2 "Ever banked (Incl. Mpesa)"
lab var has_bank_product2 "Currently banked (Incl. Mpesa)"

*** Does income, etc. explain this result? ****

* Income may impact our outcomes in a nonlinear way. Allow for a cubic specification
gen double monthly_income_sq = earned_income^2
gen double monthly_income_cube = earned_income^3
*** Distance to bank products ***

*** These are categorial, so use a vector of dummies ***
** Be sure to include dummies for don't know **
tab time_to_bank_branch, nol
* Don't know distance *
replace time_to_bank_branch = 10 if q3a_2 == 10
* Don't know closest
replace time_to_bank_branch = 11 if q3a_1 == 8

replace time_to_mpesa = 10 if q3b_2 == 10
replace time_to_mpesa = 11 if q3b_1 == 8

replace time_to_bank_agent = 10 if q4_2 == 10
replace time_to_bank_agent = 11 if q4_1 == 8

replace time_to_fin_serv_provid = 10 if q5_2 == 10
replace time_to_fin_serv_provid = 11 if q5_1 == 8

* Merge on change to access 2009-2016 *
merge m:1 province using "$d0\2009_2015_access_change.dta", nogen

gen FPExaccess = .

preserve
keep if sample2 == 1
compress

eststo clear

local i = 0

foreach var of varlist fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {
local i = `i' + 1

replace Intensity = never_attend
replace FPExintensity = FPE * Intensity


* Baseline Specification *
xi: cgmwildboot2 `var' FPExintensity female ///
age_16 age_17 age_18 age_28 age_29 i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' FPExintensity female ///
age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Control for Religion & Marital Status *

xi: cgmwildboot2 `var' FPExintensity i.a_9 i.a_11 female ///
age_16 age_17 age_18 age_28 age_29 i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) bootcluster(sub_region) reps(999) seed(1685) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo b`i': areg `var' FPExintensity i.a_9  i.a_11 female ///
age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A


if "`var'" != "econ_self_suff_kling_index" {

* Control for Income

xi: cgmwildboot2 `var' FPExintensity earned_income monthly_income_sq monthly_income_cube  ///
female age_16 age_17 age_18 age_28 age_29 i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) bootcluster(sub_region) reps(999) seed(1685) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo c`i': areg `var' FPExintensity earned_income monthly_income_sq monthly_income_cube  ///
female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Control for primary money source

xi: cgmwildboot2 `var' FPExintensity farming_most_money employed_most_money ///
casual_most_money self_employ_most_money family_friends_most_money female  ///
age_16 age_17 age_18 age_28 age_29 i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) bootcluster(sub_region) reps(999) seed(1685) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo d`i': areg `var' FPExintensity farming_most_money employed_most_money ///
casual_most_money self_employ_most_money family_friends_most_money female  ///
age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A
}
* Control for Distance to Bank Products 

local j = 1
foreach var2 of varlist time_to_bank_branch time_to_bank_agent time_to_mpesa  /*time_to_fin_serv_provid*/ {

xi: cgmwildboot2 `var' FPExintensity i.`var2' female  ///
age_16 age_17 age_18 age_28 age_29 i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) bootcluster(sub_region) reps(999) seed(1685) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo e`i'`j': areg `var' FPExintensity i.`var2' female  ///
age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

local j = `j' + 1
}

* Control for all 3 Distance to Bank Products 

xi: cgmwildboot2 `var' FPExintensity i.time_to_bank_branch i.time_to_bank_agent i.time_to_mpesa female  ///
age_16 age_17 age_18 age_28 age_29 i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) bootcluster(sub_region) reps(999) seed(1685) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

 
eststo e`i'`j': areg `var' FPExintensity i.time_to_bank_branch i.time_to_bank_agent i.time_to_mpesa female  ///
age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A
local label : variable label `var'
local lab`i' = "`label'"

* Control for change in distance to banking products at province level, 2009-2016
local j = `j' + 1

replace FPExaccess = FPE*intensity_ordinal

xi: cgmwildboot2 `var' FPExintensity FPExaccess female  ///
age_16 age_17 age_18 age_28 age_29 i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) bootcluster(sub_region) reps(999) seed(1685) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	
 
eststo e`i'`j': areg `var' FPExintensity FPExaccess female  ///
age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A
local label : variable label `var'
local lab`i' = "`label'"
		
}

label var FPExintensity "`lab1'"

estout a1 b1 c1 d1 e11 e12 e13 e14 e15 using "$dtab/table6.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

label var FPExintensity "`lab2'"

estout a2 b2 c2 d2 e21 e22 e23 e24 e25 using "$dtab/table6.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3)))  keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	
	
label var FPExintensity "`lab3'"

estout a3 b3  e31 e32 e33 e34 e35 using "$dtab/table6.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3)))  ///
	keep(FPExintensity) 	style(tex) label mlabels(,none) collabels(, none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01)	extracols(3 3)

estout a2 b2 c2 d2 e21 e22 e23 e24 e25 using "$dtab/table6.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3))) s(N, labels("Observations") ///
	fmt(0)) keep( ) 	style(tex) label mlabels(,none) collabels(, none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01)

restore

********************************************************************************
****					Table 7: OLS and 2SLS Results						****
********************************************************************************
preserve
keep if sample2 == 1

local i = 0
eststo clear
foreach var of varlist fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {

local i = `i' + 1

replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

***** OLS *****
xi: cgmwildboot2 `var' educ_yrs_censored female age_16 age_17 age_18 age_28 age_29 i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
matrix A = e(p)

eststo a`i': areg `var' educ_yrs_censored female i.age [pw = popwgt_raw] if sample2 == 1, cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

***** 2SLS *****

eststo b`i': xtivreg2 `var' (educ_yrs_censored = FPExintensity) female age_16 age_17 age_18 age_28 age_29 ///
[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) first fe i(sub_region) savefirst savefprefix(_all_)

matrix A = e(first)
local r = rownumb(matrix(A), "APF")
estadd scalar apF = A[`r',1]

local label : variable label `var'
local lab`i' = "`label'"

}

label var educ_yrs_censored "`lab1'"

estout a1 b1 using "$dtab/table7.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(educ_yrs_censored)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

label var educ_yrs_censored "`lab2'"	
	
estout a2 b2 using "$dtab/table7.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(educ_yrs_censored)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	

label var educ_yrs_censored "`lab3'"	
	
estout a3 b3 using "$dtab/table7.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) s(N apF, labels("Observations" "1st Stage F-Stat") fmt(0 2)) keep(educ_yrs_censored)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	///
	prefoot(" & & \\")

eststo clear

restore

********************************************************************************
****					Table 8: Robustness Checks							****
********************************************************************************

preserve
keep if sample2 == 1

local i = 0
foreach var of varlist educ_yrs_censored fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {

local i = `i' + 1
********* Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

* Set seeds to match with tables 3 and 4
if "`var'" == "educ_yrs_censored" {
	local s = 1211
}	
else {
	local s = 75019
}	

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(`s') ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' FPExintensity female i.age [pw = popwgt_raw] ///
if sample2 == 1, cluster(sub_region) absorb(sub_region)
count if e(sample)
local n_a = `r(N)'
estadd matrix cgm = A

local label : variable label `var'
local lab`i' = "`label'"

}

		
label var FPExintensity "Baseline $(N=`n_a')$"

estout a1 a2 a3 a4 using "$dtab/table8_baseline.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)


	
foreach x in 16 17 18 28 29 30 {
cap gen age_`x' = age == `x'
cap gen unemployment_rateXage_`x' = unemployment_rate * age_`x'
}

compress

eststo clear
** Loop Through different Robustness Checks **
foreach r in unemployment nocity migrant no_prim_int gend_int pot_yrs_prim phone internet {

* Control for 1999 Unemployment Rate *
if "`r'" == "unemployment" {
	local ctrl = "unemployment_rateXage_16 unemployment_rateXage_17 unemployment_rateXage_18 unemployment_rateXage_28 unemployment_rateXage_29"
	local rob_lab = "1999 Subregion unemployment"
}
* Control for mobile phone *
else if "`r'" == "phone" {
	local ctrl = "mobile_phone"
	local rob_lab = "Mobile phone ownership"
}
* Control for internet usage *
else if "`r'" == "internet" {
	local ctrl = "internet_usage"
	local rob_lab = "Internet usage"
}
else {
	local ctrl = ""
}

* Exclude Nairobi and Mombasa *
if "`r'" == "nocity" {
	local nair_momb = "& sub_region!=1 & sub_region!=3"
	local rob_lab = "Exclude Nairobi and Mombasa"
}
else {
	local nair_momb = ""
}

* Exclude Migrants *
if "`r'" == "migrant" {
	local mig = "& a_12 == 2"
	local rob_lab = "Exclude migrants"
}
else {
	local mig = ""
}

eststo clear
local i = 0
* Loop through different education outcomes *
foreach var of varlist educ_yrs_censored fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index  {
local i = `i' + 1

********* Dummy Interacted with Intensity ********

* Use didn't complete primary as intensity *
if "`r'" == "no_prim_int" {
	replace Intensity = no_primary
	local rob_lab = "Completed primary intensity"
}
* Gendered intensities
else if "`r'" == "gend_int" {
	replace Intensity = never_attend_female if female == 1
	replace Intensity = never_attend_male if female == 0
	local rob_lab = "Alternative intensities"
}
* Potential years of primary intensities
else if "`r'" == "pot_yrs_prim" {
	replace Intensity = potential_yrs_prim 
	local rob_lab = "Potential years of primary intensity"
}
* Baseline intensities
else {
	replace Intensity = never_attend
}

replace FPExintensity = FPE * Intensity

if "`r'" == "nocity" | "`r'" == "no_prim_int" | "`r'" == "gend_int" |"`r'" == "migrant" |"`r'" == "pot_yrs_prim" {

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region `ctrl' ///
 if sample2 == 1 `nair_momb' `mig' [pw = popwgt_raw], cluster(sub_region) ///
 bootcluster(sub_region) reps(999) seed(1211) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
}

else if "`r'" == "unemployment" {

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region `ctrl' ///
 if sample2 == 1 `nair_momb' `mig' [pw = popwgt_raw], cluster(sub_region) ///
 bootcluster(sub_region) reps(999) seed(1211) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )
}

else  {

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region `ctrl' ///
 if sample2 == 1 `nair_momb' `mig' [pw = popwgt_raw], cluster(sub_region) ///
 bootcluster(sub_region) reps(999) seed(1211) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
}

matrix A = e(p)	

eststo a`i': areg `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 `ctrl' [pw = popwgt_raw] ///
if sample2 == 1 `nair_momb' `mig', cluster(sub_region) absorb(sub_region)
count if e(sample)
local n_a = `r(N)'
estadd matrix cgm = A

local label : variable label `var'
local lab`i' = "`label'"
		
}

	
label var FPExintensity "`rob_lab' $(N=`n_a')$"

estout a1 a2 a3 a4 using "$dtab/table8_`r'.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01) 
	
label var FPExintensity "`rob_lab' $(N=`n_f')$"	
	
}	

restore

***** Free secondary education and poverty rate ******

preserve
keep if sample2 == 1
compress

tab age, gen(age_)
drop age_6

foreach check of varlist poverty_rate fse  {

* Generate interaction for robustness check *	
cap gen FPEx`check' = FPE * `check'	
local i = 1
eststo clear
foreach var of varlist educ_yrs_censored fin_incl_kling_index fin_capib_kling_index ///
	econ_self_suff_kling_index {

eststo a`i': areg `var' FPExintensity female age_? FPEx`check' [aw = popwgt_raw] ///
if sample2 == 1 , cluster(sub_region) absorb(sub_region)

count if e(sample)
local n_a = `r(N)'
boottest FPExintensity, seed(77450) nograph reps(999)
estadd local cgm = "[" + string(round(`r(p)',.001),"%9.3f") + "]"

local i = `i' + 1
}
local label : variable label `check'

label var FPExintensity "`label' $(N=`n_a')$"

estout a* using "$dtab/table8_`check'.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3))) s(cgm,label(" ")) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

}	

restore


***** Likelihood still in school in each province ****
replace dhs_2014_attend = 0 if age>25 & !mi(age) & mi(dhs_2014_attend)
gen FPExattend = FPE*dhs_2014_attend
assert !mi(FPExattend) if sample2==1

eststo clear
foreach var of varlist educ_yrs_censored fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {

xi: cgmwildboot2 `var' FPExintensity FPExattend female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
 if sample2 == 1  [pw = popwgt_raw], cluster(sub_region) ///
 bootcluster(sub_region) reps(999) seed(415) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	 
 
eststo: areg `var' FPExintensity FPExattend female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) absorb(sub_region)

count if e(sample)
local n_a = `r(N)'
estadd matrix cgm = A

local label : variable label `var'
local lab`i' = "`label'"
	
*boottest FPExintensity, nograph seed(1685) reps(999)
	
}

label var FPExintensity "Likelihood still in school $(N=`n_a')$"

estout using "$dtab/table8_school_attend.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

************************ Donald-Lang Method for SE's ***************************

**** Donald-Lang Program ****
capture program drop dl2step
program define dl2step, eclass 
	syntax varlist [if] [in] [pw aw] , treatment(varlist) period(varlist) subgroup(varlist) [first]
	
	tempvar regvar treatvar pervar subvar yhat

	marksample touse
	qui egen `regvar'=rowmiss(`varlist')
	qui egen `treatvar'=rowmiss(`treatment')
	qui egen `pervar'=rowmiss(`period')
	qui egen `subvar'=rowmiss(`subgroup')
	qui replace `touse'=(`touse' & `treatvar'==0 & `pervar'==0 & `subvar'==0)	
	
	/* deal with weights */
	if "`weight'"!="" {
		local weight "[`weight'`exp']"
	} 
	else {
		local weight ""
	}

	/* Display first step? */
	if "`first'"!="" {
		local first ""
	}
	else {
		local first "qui "
	}
	
** First Step **

* allow for weighting in first step to get weighted average within FPE X Subregion *
	
`first' reg `varlist' `period'#i.`subgroup' if `touse' `weight'

qui predict `yhat' if e(sample)

** Second Step **
preserve
	
	* One observations per FPE - subregion *
	qui keep if `touse'
	keep `subgroup' `period' `treatment' `yhat' 
	qui duplicates drop
	isid `subgroup' `period'
	
	* Generate interaction of interest *
	qui gen `period'x`treatment' = `period' * `treatment'
	
	reg `yhat' `period'x`treatment' `period' `treatment', cluster(`subgroup')
	
restore	

end

** Donald-Lang RF Results **

* Loop through different outcomes *
eststo clear
local i = 0
foreach var of varlist educ_yrs_censored fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {
local i = `i' + 1


********* Dummy Interacted with Intensity ********
replace Intensity = never_attend

eststo a`i': dl2step `var' if sample2 == 1 [pw = popwgt_raw], treatment(Intensity) ///
period(FPE) subgroup(sub_region) 
count if e(sample)
local n_a = `r(N)'

	
}

cap gen FPExIntensity = .	
	
label var FPExIntensity "Donald-Lang two step estimation $(N=26)$"

estout a1 a2 a3 a4 using "$dtab/table8_donald_lang_se.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)))  keep(FPExIntensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	postfoot(" & & & \\")

******************** Older Cohorts Falsification *******************************	
preserve
gen placebo_sample = (age>=28 & age<=30)  | (age>=40 & age<=42)

**************** Kling indices for Falsificaton Cohort *************************

cap drop fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index

** The Kling indices demean the variable with respect to the control group **
** Our ``control" group will be those 40-42 in below median intensity sub-regions **

su never_attend [aw=popwgt_raw], d

cap drop ctrl
gen ctrl = never_attend<=`r(p50)' & (age>=40 & age<=42)


*********************** Financial Inclusion Index ******************************

local i = 0
foreach var of varlist ever_banked2 has_bank_product2 ever_formal_savings_product {

local i = `i' + 1

sum `var'   [aw = popwgt_raw] if ctrl == 1
gen fin_incl_index_`i' = (`var' - r(mean))/r(sd)  

}


egen fin_incl_kling_index = rowmean(fin_incl_index_*)

drop fin_incl_index_*


************************* Financial Capability Index ***************************

local i = 0
foreach var of varlist b2 numeracy2 retire_own_plans informal_savings_group emergency_funds ///
safe_place_money any_savings {

local i = `i' + 1

sum `var'   [aw = popwgt_raw] if ctrl == 1
gen fin_capib_index_`i' = (`var' - r(mean))/r(sd)  

}


egen fin_capib_kling_index = rowmean(fin_capib_index_*)

drop fin_capib_index_*

************************ Economic Self-Sufficiency Index ***********************

local i = 0
foreach var of varlist earned_any_income earned_income income_independence {

local i = `i' + 1

sum `var'   [aw = popwgt_raw] if ctrl == 1
gen econ_self_suff_index_`i' = (`var' - r(mean))/r(sd)  

}

egen econ_self_suff_kling_index = rowmean(econ_self_suff_index_*)
drop econ_self_suff_index_*

lab var fin_incl_kling_index "Formal Financial Inclusion"
lab var fin_capib_kling_index "Financial Capability"
lab var econ_self_suff_kling_index "Economic Self-Sufficiency"


******************* Reduced Form Falsification Analysis ********************

gen FPE_falsification = (age>=28 & age<=30)
lab var FPE_falsification "FPE (Falsification)"

keep if placebo_sample == 1

gen age_40 = age==40
gen age_41 = age==41

local i = 0
foreach var of varlist educ_yrs_censored fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {

local i = `i' + 1

********* Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE_falsification * Intensity

xi: cgmwildboot2 `var' FPExintensity female age_28 age_29 age_30 age_40 age_41 i.sub_region  ///
 if placebo_sample == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' FPExintensity female age_28 age_29 age_30 age_40 age_41 [pw = popwgt_raw] ///
if placebo_sample == 1, cluster(sub_region) absorb(sub_region)
count if e(sample)
local n_a = `r(N)' 
estadd matrix cgm = A

}
	
label var FPExintensity "Placebo for 28-30 vs. 40-42 $(N=`n_a')$"

estout a1 a2 a3 a4 using "$dtab/table8_FPE_falsification.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

restore	

********************************************************************************
**** Figure 1: Share of 15-25 year olds that never attended primary school (1999 Census) ****
********************************************************************************

*** See do file: figure1_map.do ***

********************************************************************************
**** Figure 2: Coefficients on Interactions of Age and FPE Intensity for Years of Education ****
**** Figure 3: Coefficients on Interactions of Age and FPE Intensity for Summary Indices ****
********************************************************************************

gen coef = .
gen coef_ub = .
gen coef_lb = .

gen age_at_fpe = age-12
lab var age_at_fpe "Age in 2003"

* Define upper bound for graphs *
local ub = 28
local ubn = `ub'-1

cap drop IntensityXage_*

forv x = 4(1)27 {
cap gen age_at_fpe`x' = age_at_fpe == `x'
cap gen IntensityXage_`x' = age_at_fpe`x' * Intensity
}

foreach z in educ_yrs_censored fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {

replace coef = 0 if age_at_fpe == 28

cap drop n
bys age: gen n = _n

if "`z'" == "fin_incl_kling_index" {
	local ylab = "ylab(-1(1)2)"
	}

else if "`z'" == "fin_capib_kling_index" {
	local ylab = "ylab(-1(1)1)"
	}

else if "`z'" == "econ_self_suff_kling_index" {
	local ylab = "ylab(-2(1)2)"
	}
	
else {
		local ylab = ""
	}

replace Intensity = never_attend
forv x = 4(1)27 {
replace IntensityXage_`x' = age_at_fpe`x' * Intensity
lab var IntensityXage_`x' "`x'"
}

areg `z' IntensityXage_* age_at_fpe* female [pw = popwgt_raw] if age_at_fpe<=28, cluster(sub_region) absorb(sub_region)

forv x = 4(1)27 {
replace coef = _b[IntensityXage_`x'] if age_at_fpe == `x'
replace coef_ub = _b[IntensityXage_`x'] + 1.96*_se[IntensityXage_`x'] if age_at_fpe == `x'
replace coef_lb = _b[IntensityXage_`x'] - 1.96*_se[IntensityXage_`x'] if age_at_fpe == `x'
}

twoway (connected coef age_at_fpe, lp(solid) lc(black) mc(black)) (line coef_ub age_at_fpe, lp(dash) lc(black)) ///
 (line coef_lb age_at_fpe, lp(dash) lc(black)) if age_at_fpe<=28 & n == 1, graphregion(color(white)) xlab(4(2)28) ///
 legend(off) yline(0, lp(solid) lc(black)) ylabel(,nogrid) `ylab'
if "`z'" == "educ_yrs_censored" {
	graph export "$dtab/figure2.eps", replace
}
else {
	graph export "$dtab/figure3_`z'.eps", replace
}	
}


cap log close


